﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WaterRevenueMS.WebUI.Extension.Filters;
using WaterRevenueMS.Domain.ViewModels.BasicSet.BankCode;
using System.ComponentModel.Composition;
using WaterRevenueMS.Domain.Common.Models;
using WaterRevenueMS.WebUI.Common;
using System.Linq.Expressions;
using WaterRevenueMS.Componnent.Entities.BasicSet;
using WaterRevenueMS.WebUI.Extension.MvcHelper;
using WaterRevenueMS.Componnent.Common;
using WaterRevenueMS.Core.Service.BasicSet;
using System.Web.UI;

namespace WaterRevenueMS.WebUI.Controllers
{
    [Export]
    [PartCreationPolicy(CreationPolicy.NonShared)]
    public class BankCodeController : AdminController
    {
        [Import]
        public IBankCodeService BankCodService { get; set; }

        [AdminLayoutAttribute]
        public ActionResult Index()
        {
            var model = new BankCodeModel();
            return View(model);
              
        }

        [AdminPermission(PermissionCustomMode.Ignore)]
        public ActionResult List(DataTableParameter param)
        {
            int total = BankCodService.BankCodeS.Count(t => t.IsDeleted == false );

            var expr = BuildSearchCriteria();

            var filterResult = BankCodService.BankCodeS.Where(expr).Select(t => new BankCodeModel
            {
                Id= t.Id,
                Yin_HMC = t.Yin_HMC,
                Yin_HJC = t.Yin_HJC,
                OrderNo = t.OrderNo


            }).OrderBy(t => t.Id).Skip(param.iDisplayStart).Take(param.iDisplayLength).ToList();

            int sortId = param.iDisplayStart + 1;

            var result = from c in filterResult
                         select new[]
                        {
                            sortId++.ToString(), 
                            c.Yin_HMC,
                            c.Yin_HJC,  
                            c.OrderNo.ToString(),
                            c.Id.ToString()
                        };

            return Json(new
            {
                sEcho = param.sEcho,
                iDisplayStart = param.iDisplayStart,
                iTotalRecords = total,
                iTotalDisplayRecords = total,
                aaData = result
            }, JsonRequestBehavior.AllowGet);
        }

        public ActionResult Create()
        {
            var model = new BankCodeModel();
           
            return PartialView(model);
        }

        [HttpPost]
        [AdminOperateLog]
        public ActionResult Create(BankCodeModel model)
        {
            if (ModelState.IsValid)
            {
                OperationResult result = BankCodService.Insert(model);
                if (result.ResultType == OperationResultType.Success)
                {
                    return Json(result);
                }
                else
                {
                    return PartialView(model);
                }
            }
            else
            {
                return PartialView(model);
            }
        }

        public ActionResult Edit(int Id)
        {
            var model = new BankCodeModel();
            var entity = BankCodService.BankCodeS.FirstOrDefault(t => t.Id == Id);
            if (null != entity)
            {
                model = new BankCodeModel
                {
                    Id = entity.Id,
                    Yin_HMC = entity.Yin_HMC,
                    
                    Yin_HJC = entity.Yin_HJC,
                    OrderNo = entity.OrderNo

                };
            }
            return PartialView(model);
        }

        [HttpPost]
        public ActionResult Edit(BankCodeModel model)
        {
            if (ModelState.IsValid)
            {
                OperationResult result = BankCodService.Update(model);
                if (result.ResultType == OperationResultType.Success)
                {
                    return Json(result);
                }
                else
                {
                    return PartialView(model);
                }

            }
            else
            {
                return PartialView(model);
            }
        }


        public ActionResult Delete(int Id)
        {
            OperationResult result = BankCodService.Delete(Id);
            return Json(result);
        }

        [AdminPermission(PermissionCustomMode.Ignore)]
        [OutputCache(Location = OutputCacheLocation.None, NoStore = true)]
        public JsonResult IsUnique_Available(string Yin_HMC)
        {
            if (!string.IsNullOrEmpty(Yin_HMC))
            {
                var entity = BankCodService.BankCodeS.FirstOrDefault(t => t.Yin_HMC == Yin_HMC);
                if (entity != null)
                {
                    
                    return Json(false, JsonRequestBehavior.AllowGet);
                }
                else
                {
                    return Json(true, JsonRequestBehavior.AllowGet);
                }
            }
            return Json(true, JsonRequestBehavior.AllowGet);
        }

        [AdminPermission(PermissionCustomMode.Ignore)]
        [OutputCache(Location = OutputCacheLocation.None, NoStore = true)]
        public JsonResult IdUnique_Available(int Id)
        {
          //  if (!string.IsNullOrEmpty(Id))
            {
                var entity = BankCodService.BankCodeS.FirstOrDefault(t => t.Id == Id);
                if (entity != null)
                {

                    return Json(false, JsonRequestBehavior.AllowGet);
                }
                else
                {
                    return Json(true, JsonRequestBehavior.AllowGet);
                }
            }
            return Json(true, JsonRequestBehavior.AllowGet);
        }

        private Expression<Func<BankCode, Boolean>> BuildSearchCriteria()
        {
            DynamicLambda<BankCode> bulider = new DynamicLambda<BankCode>();
            Expression<Func<BankCode, Boolean>> expr = null;
            if (!string.IsNullOrEmpty(Request["Yin_HMC"]))
            {
                var data = Request["Yin_HMC"].Trim();
                Expression<Func<BankCode, Boolean>> tmp = t => t.Yin_HMC.Contains(data);
                expr = bulider.BuildQueryAnd(expr, tmp);
            }
            if (!string.IsNullOrEmpty(Request["Yin_HJC"]) && Request["Yin_HJC"] != "0")
            {
                var data = Request["Yin_HJC"].Trim();
                Expression<Func<BankCode, Boolean>> tmp = t => t.Yin_HMC.Contains(data);
                expr = bulider.BuildQueryAnd(expr, tmp);
            }

            Expression<Func<BankCode, Boolean>> tmpSolid = t => t.IsDeleted == false;
            expr = bulider.BuildQueryAnd(expr, tmpSolid);

            return expr;
        }

      


    }
}
